package com.spring.main.aop;

import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@Service(value = "demo")
public class DemoImpl implements IDemo {

    @AProxy(value = "doBefore")
    @Override
    public void demo() {
        System.out.println("正常方法");
    }

    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException, InstantiationException {
        Class clazz = DemoImpl.class;
        Method[] methods = clazz.getMethods();
        String methodName = null;
        Method method1 = null;
        for (Method method : methods) {
            if (method.isAnnotationPresent(AProxy.class)) {
                method1 = method;
                AProxy aProxy = method.getAnnotation(AProxy.class);
                methodName = aProxy.value();
            }
        }
        if (null != methodName) {
            ProxyHandler proxyHandler = new ProxyHandler();
            Method method = clazz.getMethod(methodName);
            proxyHandler.demo(clazz, method);
            proxyHandler.demo(clazz, method1);
        }
    }

    public void doBefore() {
        System.out.printf("开始");
    }
}
